﻿using Foxit.PDF;
using Foxit.PDF.PageElements;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.Reporting.Framework
{
    public class PdfReport
    {
        #region Public Properties

        public Document Document { get; set; }

        public PageSize PageSize { get; set; }
        public PageOrientation PageOrientation { get; set; }
        public float PageMargins { get; set; }
        public PageDimensions PageDimensions { get; set; }
        // Page Dimensions of pages


        public float FooterHeight { get; set; }
        public float LeftMargin { get; set; }
        public float PageMaxWidth { get; set; }
        public float PageMaxHeight { get; set; }
        public bool ShowLayoutGrid { get; set; }

        public List<PdfReportPage> Pages { get; set; }

        private int _pageCounter;
        public int PageCounter
        {
            get { return _pageCounter; }
            set { _pageCounter = value; }
        }

        public Outline ParentOutline { get; set; }

        #endregion


        public PdfReport()
        {
            // Create a _document and set it's properties
            Document = new Document
            {
                Creator = "Sustainalytics",
                Author = "Sustainalytics",
                Title = "Company Report"
            };

            PageSize = PageSize.A4;
            PageOrientation = PageOrientation.Portrait;
            PageMargins = 20f;
            PageDimensions = new PageDimensions(PageSize, PageOrientation, PageMargins);

            Pages = new List<PdfReportPage>();

            // Add the top level outline to the _document
            ParentOutline = Document.Outlines.Add("Content");
            ParentOutline.Color = new WebColor("silver");
            ParentOutline.Style = TextStyle.Bold;

            _pageCounter = 0;

            FooterHeight = 32f;
            LeftMargin = 10f;

            PageMaxWidth = PageDimensions.Body.Width - LeftMargin;
            PageMaxHeight = PageDimensions.Body.Height - FooterHeight;
            ShowLayoutGrid = false;
        }

        public void AddPage(Page page)
        {
            Document.Pages.Add(page);
            _pageCounter++;
        }

        public void AddPages(List<Page> pages)
        {
            pages.ForEach(p =>
            {
                Document.Pages.Add(p);
                _pageCounter++;
            });
        }

        // Render the document
        public byte[] Draw()
        {
            if (Pages.Any())
                Pages.OrderBy(p => p.PageNumber).ToList()
                     .ForEach(p => Document.Pages.Add(p));

            var buffer = Document.Draw();

            return buffer;
        }

        public PdfReportPage NewPage()
        {
            return new PdfReportPage(this);
        }

        public void AddPages(List<PdfReportPage> pages)
        {
            pages.ForEach(p =>
            {
                Pages.Add(p);
                PageCounter++;
                p.PageNumber = PageCounter;
            });
        }
    }

    public class PdfReportPage : Page
    {
        public PdfReport Report { get; set; }
        public int PageNumber { get; set; }
        public float Top { get; set; }

        public PdfReportPage(PdfReport report)
        {
            Dimensions = new PageDimensions(report.PageSize, report.PageOrientation, report.PageMargins);

            if (report.ShowLayoutGrid)
                Elements.Add(new LayoutGrid { ShowTitle = false });

            Report = report;
            Top = 0.0f;
        }

        public void AddElement(PageElement element)
        {
            Elements.Add(element);
        }
    }
}
